@echo off
setlocal enabledelayedexpansion

REM 在这里添加每个项目自己的文件路径（不能有空格）
set output_dir=..\client\ProjectAll\Assets\ClientRes\script\communication

echo output dir is %output_dir%

rd /s /q ProtoBuf\generate\
rem rd /s /q %output_dir%\
if exist %output_dir% (
	del /f /s /q %output_dir%\*.cs
) else (
	mkdir %output_dir%\
)
mkdir ProtoBuf\generate\
mkdir ProtoBuf\generate\csharp\

REM ------------------------------------------------------------------------------------------
echo --- Performing pre-process cleanup... ---

cd ProtoBuf\ProtoGen\
del /q *.proto 
cd ..\..

cd ProtoBuf\ProtoFile\
del /q *.proto 
cd ..\..

REM ------------------------------------------------------------------------------------------
echo --- Copying all .proto files into \ProtoBuf\ProtoGen\ and \ProtoBuf\ProtoFile\ ---

cd ProtoFile

for /f "tokens=* delims= " %%a in ('dir /b *.proto') do (
echo copying %%~a
copy %%~a ..\ProtoBuf\ProtoGen\%%~a
copy %%~a ..\ProtoBuf\ProtoFile\%%~a
)

cd ..

REM ------------------------------------------------------------------------------------------
echo --- calling protogen.exe for all copied .proto files ---

cd ProtoBuf\ProtoGen\

set in_files=
set in_files_tmp1=-i:
for /f "tokens=* delims= " %%a in ('dir /b *.proto') do (
set "in_files= %in_files_tmp1%%%~a !in_files! "
)
echo %in_files%
@echo off
set startime=%time%

protogen.exe %in_files% -o:../generate/csharp/PBMessage.cs -ns:ProtoBuf.Message

set endtime=%time%
echo %startime% End%endtime%
echo generate over

cd ..\..

echo Begin generate OpCode

REM ------------------------------------------------------------------------------------------
echo --- calling PBGenerator.exe for all copied .proto files ---

cd ProtoBuf\ProtoGen\

set proto_files=

for /f "tokens=* delims= " %%a in ('dir /b *.proto') do (
set "proto_files=!proto_files! %%~a"
)

cd ..

PBGenerator.exe %proto_files% protobuf.message ProtoBuf.Message

cd ..

REM ------------------------------------------------------------------------------------------
echo --- Copying all .proto files ---

cd ProtoBuf\ProtoGen\

echo copying OpDefine.cs
copy ..\generate\OpDefine.cs  ..\..\%output_dir%\OpDefine.cs
echo copying PBConvert.cs
copy ..\generate\PBConvert.cs ..\..\%output_dir%\PBConvert.cs

copy ..\generate\csharp\PBMessage.cs ..\..\%output_dir%\PBMessage.cs
rem for /f "tokens=* delims= " %%a in ('dir /b *.proto') do (
rem echo copying %%~a
rem copy ..\generate\csharp\%%~na.cs ..\..\%output_dir%\%%~na.cs
rem )

cd ..\..

REM ------------------------------------------------------------------------------------------
echo --- Performing post-process cleanup... ---

rd /s /q ProtoBuf\generate\

cd ProtoBuf\ProtoGen\
del /q *.proto 
cd ..\..

cd ProtoBuf\ProtoFile\
del /q *.proto 
cd ..\..

pause